using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using Umdf.SideShow.Events;
using System.Net.Sockets;

namespace Umdf.SideShow.PocketPC
{
    class PocketPCServerChannel : PocketPCChannelBase, IPocketPCChannel
    {
        private ICallbackChannel _callback;

        public PocketPCServerChannel(TcpClient client, ICallbackChannel callback) : base(client)
        {
            _callback = callback;
        }

        protected override void OnDataReceived(byte[] data)
        {
            using (MemoryStream ms = new MemoryStream(data))
            {
                using (BinaryReader reader = new BinaryReader(ms))
                {
                    Guid cmd = new Guid(reader.ReadBytes(16));
                    if (cmd == ChannelMessages.PostEvent)
                    {
                        string typeName = reader.ReadString();
                        Type t = Type.GetType(typeName);
                        SideShowEvent ev = (SideShowEvent) Activator.CreateInstance(t, reader);
                        _callback.PostEvent(ev);
                    }
                }
            }
        }

        #region IServerChannel Members

        public void AddApplication(SideShowApplication app)
        {
            using (MemoryStream ms = new MemoryStream())
            {
                using (BinaryWriter writer = new BinaryWriter(ms))
                {
                    writer.Write(ChannelMessages.AddApplication.ToByteArray());
                    app.SerializeObject(writer);
                    Send(ms.ToArray());
                }
            }
        }

        public void AddContent(Guid applicationId, Guid sideShowEndpointId, Umdf.SideShow.Contents.Content newContent)
        {
            using (MemoryStream ms = new MemoryStream())
            {
                using (BinaryWriter writer = new BinaryWriter(ms))
                {
                    writer.Write(ChannelMessages.AddContent.ToByteArray());
                    writer.Write(applicationId.ToByteArray());
                    writer.Write(sideShowEndpointId.ToByteArray());
                    writer.Write(newContent.GetType().FullName);
                    newContent.SerializeObject(writer);
                    Send(ms.ToArray());
                }
            }
        }

        public void RemoveApplication(Guid appId)
        {
            using (MemoryStream ms = new MemoryStream())
            {
                using (BinaryWriter writer = new BinaryWriter(ms))
                {
                    writer.Write(ChannelMessages.RemoveApplication.ToByteArray());
                    writer.Write(appId.ToByteArray());
                    Send(ms.ToArray());
                }
            }
        }

        public void RemoveContent(Guid applicationId, Guid sideShowEndpointId, uint removedContentId)
        {
            using (MemoryStream ms = new MemoryStream())
            {
                using (BinaryWriter writer = new BinaryWriter(ms))
                {
                    writer.Write(ChannelMessages.RemoveContent.ToByteArray());
                    writer.Write(applicationId.ToByteArray());
                    writer.Write(sideShowEndpointId.ToByteArray());
                    writer.Write(removedContentId);
                    Send(ms.ToArray());
                }
            }
        }

        public void Initialize()
        {
            using (MemoryStream ms = new MemoryStream())
            {
                using (BinaryWriter writer = new BinaryWriter(ms))
                {
                    writer.Write(ChannelMessages.Initialize.ToByteArray());
                    Send(ms.ToArray());
                }
            }
        }

        public void Deinitialize()
        {
            using (MemoryStream ms = new MemoryStream())
            {
                using (BinaryWriter writer = new BinaryWriter(ms))
                {
                    writer.Write(ChannelMessages.Deinitialize.ToByteArray());
                    Send(ms.ToArray());
                }
            }
            
        }

        

        #endregion
    }
}
